/*
https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/solution/liang-ge-lian-biao-de-di-yi-ge-gong-gong-pzbs/
 */
public class SolutionOffer52 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA==null || headB==null){
            return null;
        }
        ListNode pa=headA;
        ListNode pb=headB;
        while (pa!=pb){
            pa=pa==null?headB:pa.next;
            pb=pb==null?headA:pb.next;
        }
        return pa;
    }

    public static void main(String[] args) {
        ListNode node1=new ListNode(4);
        ListNode node2=new ListNode(1);
        ListNode node3=new ListNode(8);
        ListNode node4=new ListNode(4);
        ListNode node5=new ListNode(1);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node5.next=node3;
        System.out.println(new SolutionOffer52().getIntersectionNode(node1,node5).val);
    }
}
